define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    cardInfo: {},
                    store:{},
                    loading:0,
                    cardQrcodeInfo:{},
                    downUrl:''
                    
                };
            },
            el: '#pageCardDetail',
            created: function() {
                this.fetchData();
            },
            mounted: function() {
                if (this.mounted == 1) {
                    return;
                }
                this.mounted = 1;
                app.copy("#shareClipboard",function(res){
                   console.log(res);
                   if(res=="success"){
                     app.alert("链接复制成功");
                   }else{
                     this.share();
                   }
                });
            },
            
            methods: {
                fetchData: function() {
                    var _this = this;

                    this.getInfo();
                   
                   
                },
                getCardQrcode:function(){
                  //如果是微信打开卡包
                   var user_info =  app.getUserInfo();
                   this.p_user_id = user_info.user_id;
                   this.shareUrl =window.location.origin+"/pages/wx-card/?store_id="+app.getStoreId()+"#/opencard/"+this.card_id+"/"+user_info.user_id;
                   this.show_qrcode_url= app.params.apiDomain+"/api/index/urlQRcode?url="+encodeURIComponent(this.shareUrl);
                   this.downUrl=app.params.apiDomain+"/apiv2/card/download?url="+encodeURIComponent(this.show_qrcode_url);
                },

                setWxShare:function(){
                  if(!app.isWeiXin()){
                     return ;
                  }
                  var that = this;
                  var wxShareData = {
                        title: "["+that.cardInfo.config.base_info.brand_name+"]"+that.cardInfo.config.base_info.title, // 分享标题
                        imgUrl: that.store.logo, // 分享图标
                        desc: "您的好友分享了一张卡券,快来领取吧",
                        url:this.shareUrl
                    };
                   app.wxShare(wxShareData);
                },
                //分享给好友
                share:function(){
                  var text = '复制链接发给朋友：'+this.shareUrl ;
                  // if(app.isWeiXin()){
                  //   text = "请点击微信右上角分享给好友";
                  // }
                  app.confirm({
                    text:text
                  });
                },
                getInfo:function(){
                    var that = this;
                    if( that.loading == 1){
                        return ;
                     }
                     that.loading = 1;
                    
                    var user_info =  app.getUserInfo();
                    // app.get("api/user/info", {user_id:user_info.user_id,fields:'store'},function(res){
                    //     that.store = res.data.store;
                    // });
                    app.get("apiv2/wx_card/find", {
                      id : this.$route.params.id 
                     
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.card_id = res.data.cardId;
                      that.cardInfo = res.data;
                      that.store = res.data.store;
                      that.getCardQrcode();
                      that.setWxShare();
                      
                    });
                }
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
